﻿/**********************************************
 * 功能名称：数据检查
编写人：  杨莹
建立时间： 杨 2008年11月15日
说明：

 **************************************************************/


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.Calculation
{
    public partial class DataCheck : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        List<string> MyProductAccount = new List<string>();
        List<int> MyCenterIDs = new List<int>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string MyCurrentAccountPeriod = Application["accountPeriod"].ToString();

                Response.Write("<div id=xx2  align = center ><img alt='' src='/App_Themes/2011_Blue/Images/Loading.gif' width = 100 height = 100 /><br>正在准备数据检查，请等待。。。</div>");
                string MyEmptyString = " ";
                for (int i = 0; i < 300; i++)
                {
                    MyEmptyString = MyEmptyString + " ";
                }
                Response.Write(MyEmptyString);//用于填充满256个字节。
                Response.Flush();



                db.CommandTimeout = 240;  //设定计算时间在20分钟以内完成。




                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                MyProductAccount.Clear();
                MyCenterIDs.Clear();
                var myquery = from a in db.TABC_JS_CostCenter select a;
                FillList(Convert.ToInt32(CurrentUserCenterId.责任部门ID), myquery);

                Session["list"] = MyCenterIDs;

                List<string> MyProductAccount_temp = new List<string>();
                MyProductAccount_temp = (from a in MyProductAccount
                                         select a).Distinct().ToList();
                Session["ProductList"] = MyProductAccount_temp;


                Response.Write("<script>document.getElementById('xx2').style.display='none';</script>");
            }
            else
            {
                MyCenterIDs = (List<int>)Session["list"];

                MyProductAccount = (List<string>)Session["ProductList"];
            }

      


         }

        //查询当前操作员对应的产品ID
        private void FillList(int CenterID, IEnumerable<TABC_JS_CostCenter> TotalTrees)
        {
            var Mylist = (from m in TotalTrees where m.上级作业中心ID == CenterID select m).ToList();

            if (Mylist.Count() == 0)
                return;
            foreach (var MylistItem in Mylist)
            {
                var ProductQuerys = from a in db.TABC_JS_ProductInProcess
                                    where a.作业中心ID == MylistItem.作业中心ID
                                    select a.产品ID;

                MyCenterIDs.Add(MylistItem.作业中心ID);
                if (ProductQuerys.Count() != 0)
                {
                    foreach (var ProductQuery in ProductQuerys)
                    {
                        MyProductAccount.Add(ProductQuery);
                    }
                }

                FillList(MylistItem.作业中心ID, TotalTrees);

            }
        }

        protected void Button_CheckPathMap_Click(object sender, EventArgs e)
        {
            GridView_PathCache.Visible = true;
            string MyCurrentAccountPeriod = Application["accountPeriod"].ToString();


            var MyPathCaches = from m in db.TABC_JS_ProductPathCache_Map
                              join n in db.TABC_JS_Product on m.产品ID equals n.产品账户ID
                              // where m.会计期间ID == MyCurrentAccountPeriod
                            //   && MyProductAccount_Distinct.Contains(m.产品ID)
                               where MyProductAccount.Contains(m.产品ID)
                               orderby m.产品ID
                              select new
                              {
                                  m.产品ID,
                                  n.产品账户名称,
                                  m.路径
                              };
            if (MyPathCaches.Count() == 0)
            {
                Response.Write("<script>alert('当前操作员没有权限查看中间体路径');</script>");
                return;
            }
              string[,] MyGridViewList = new string[MyPathCaches.Count(),3] ;
            int i = 0;

            foreach (var MyPathCache in MyPathCaches)
            { 
               

                string pathstring = MyPathCache.路径;
                string PathRow = "";
                if (pathstring != "" || pathstring != null)
                {
                    string[] MyCenterPIPlists = pathstring.Split(',');

                   string MyCenterlist_lastOne="";

                   foreach (string MyCenterPIPlist in MyCenterPIPlists)
                    {
                        MyCenterlist_lastOne = MyCenterPIPlist;
                        var MyCenterName = (from a in db.TABC_JS_ProductInProcess
                                            join b in db.TABC_JS_CostCenter 
                                            on a.作业中心ID equals b.作业中心ID
                                            where a.中间体ID == MyCenterPIPlist
                                           select new
                                           {
                                               a.作业中心ID,
                                               b.作业中心名称
                                           }).Single();
                        var Is_W_INDEX_Exist = from h in db.TABC_JS_ProductInProcessDelivery
                                               where h.中间体ID == MyCenterPIPlist
                                               && h.会计期间ID == MyCurrentAccountPeriod
                                               select h;
                        if (Is_W_INDEX_Exist.Count() == 0)
                        {
                            PathRow = PathRow + "→" + MyCenterName.作业中心名称 + "<font color=red>［警告：本步W系数信息未录入］</font>";
                             
                        }
                        else if (Is_W_INDEX_Exist.Count() == 1)
                        {
                        PathRow = PathRow+"→"+MyCenterName.作业中心名称;
                        }
                        
                    }

                   var Is_Product = from m in db.TABC_JS_ProductInProcess
                                    where m.中间体ID == MyCenterlist_lastOne
                                    select m.是否中间体;

                   if (Is_Product.Count() == 1)
                   {
                       if (Is_Product.Single() == true)
                       {
                           PathRow = PathRow + "<font color=red>［警告：转出路径未设置］</font>";
                       }
                   }


                
                }
                MyGridViewList[i,0] = MyPathCache.产品ID;
                MyGridViewList[i,1] = MyPathCache.产品账户名称;
                MyGridViewList[i,2] = PathRow;
                
                i++;
            }

            GridView_PathCache.DataSource = ConvertToDataTable(MyGridViewList);
            GridView_PathCache.DataBind();

            GridView_CheckOutput.Visible = false;
        }


        private DataTable ConvertToDataTable(string[,] arr)
        {
          
            DataTable dataSouce = new DataTable();
            //for (int i = 0; i < arr.GetLength(1); i++)
            //{
            DataColumn newColumn1 = new DataColumn("产品ID", arr[0, 0].GetType());
                dataSouce.Columns.Add(newColumn1);
                DataColumn newColumn2 = new DataColumn("产品名称", arr[0, 0].GetType());
                dataSouce.Columns.Add(newColumn2);
                DataColumn newColumn3 = new DataColumn("路径", arr[0, 0].GetType());
                dataSouce.Columns.Add(newColumn3);
            //}
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                DataRow newRow = dataSouce.NewRow();
                 newRow[0] = arr[i, 0];
                 newRow[1] = arr[i, 1];
                 newRow[2] = arr[i, 2];

                dataSouce.Rows.Add(newRow);
            }
            return dataSouce;

        }

        protected void Button_CheckOutput_Click(object sender, EventArgs e)
        {
            GridView_CheckOutput.Visible = true;
            string MyCurrentAccountPeriod = Application["accountPeriod"].ToString();
            var CheckOutPut = from a in db.VABC_Check_Output
                              join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                              join c in db.TABC_JS_ProductInProcess on a.中间体ID equals c.中间体ID
                              where a.会计期间ID == MyCurrentAccountPeriod
                              && MyCenterIDs.Contains(a.作业中心ID)
                              select new
                              {
                                  a.作业中心ID,
                                  b.作业中心名称,
                                  a.中间体ID,
                                  中间体名称 = (c.中间体名称.Length > 20 ?c.中间体名称.Substring(0,20)+"...":c.中间体名称),
                                 W系数页面录入产出数量 = a.W转出数量,
                                 中间体转出路径录入产出数量 = a.转出数量,
                                  是否相等 = (a.W转出数量 == a.转出数量 ? "是":"<font color=red>否</font>"),
                                  a.期初批时,
                                  a.结存批时,
                                  a.结存批数,
                                  a.损失批时,
                                  a.损失批数,
                                  a.转出批时,
                                  a.转出批数,
                                  批时检查 = ((a.结存批时+a.损失批时+a.转出批时)>=a.期初批时 ? "通过":"<font color=red>未通过</font>")
                              };
            if (CheckOutPut.Count() == 0)
            {
                Response.Write("<script>alert('当前操作员没有权限查看中间体路径或是产出数据信息为空');</script>");
                return;
            }

            GridView_CheckOutput.DataSource = CheckOutPut;

            GridView_CheckOutput.DataBind();
            GridView_PathCache.Visible = false;
            
        } 
    }
}
